﻿Public Class frmsection
    Dim Strsql As String
    Dim Mysql1 As New ClsSQLhelper
    Dim On_Load As Boolean = True
    Dim DSWHIS As New DSWHIS

    Sub Load_CategoryData()
        Try

            Strsql = "select section_id,section_name,note,active from section   order by active desc ,section_name"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DSWHIS = Mysql1.GetMYSQLDataset(Strsql, "category")
            Pn_Framework.Set_GridSetting(Me.gridsection, DSWHIS.Tables("category"))
            With gridsection
                .Columns(0).HeaderText = "ลำดับ"
                .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .Columns(1).HeaderText = "ชื่อประเภทรายการ"
                .Columns(2).HeaderText = "หมายเหตุ"
                .Columns(3).HeaderText = "เปิดใช้งาน"
            End With
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try

    End Sub
    Function Load_ComboboxData_site() As Boolean
        Try
            Dim DS As New DataSet

            Load_ComboboxData_site = False

            Strsql = "select * from site"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_site", "site")
            ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
            With Me.CboSite
                If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "site"
                .ValueMember = "siteid"
                .SelectionStart = 0
                If My.Settings.CurrentSite <> "" Then
                    .SelectedValue = My.Settings.CurrentSite
                End If
            End With
            Load_ComboboxData_site = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_site - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)

        End Try
    End Function
    Private Sub frmsection_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_CategoryData()
    End Sub

    Private Sub gridsection_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridsection.CellClick
        Try
            With gridsection
                If .RowCount = 0 Then Exit Sub
                Me.Txtid.Text = .Item(0, e.RowIndex).Value.ToString
                Me.TxtName.Text = .Item(1, e.RowIndex).Value.ToString
                Me.TxtNote.Text = .Item(2, e.RowIndex).Value.ToString
                Me.ChkActive.Checked = .Item(3, e.RowIndex).Value.ToString
            End With

        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Try
            If MsgBox("คำเตือน การลบ section จะทำให้ category ที่มีอยู่ถูกลบไปด้วย กด yes เพื่อยืนยันกด No เพื่อยกเลิก", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "คุณต้องการลบข้อมูลหรือไม่") = MsgBoxResult.No Then Exit Sub
            Strsql = "Delete from Section where Section_id = " & Me.Txtid.Text
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            Mysql1.MySQLExecute(Strsql)

            Strsql = "Delete from category where sectionid = " & Me.Txtid.Text
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            Mysql1.MySQLExecute(Strsql)
            'Refresh Data
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            If Me.TxtName.Text.Trim = "" Then Exit Sub
            If MsgBox("คุณต้องการบันทึกข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub
            Dim DS As New DataSet
            If Me.Txtid.Text = "" Then
                Strsql = "Select * from Section where Section_name= '" & Me.TxtName.Text & "'"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_Section", "Section")
                If DS.Tables(0).Rows.Count <> 0 Then
                    MsgBox("รายการ นี้มีอยู่แล้วไม่สามารถบันทึกซ้ำได้อีก", MsgBoxStyle.Critical)
                    Exit Sub
                End If
                Strsql = "Insert into section (section_name,note,siteid) values ('" & Me.TxtName.Text & "', '" & Me.TxtNote.Text & "'," & Me.CboSite.SelectedValue & ")"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)


                Dim MySectionID As Integer
                Strsql = "select * from  section where siteid = " & Me.CboSite.SelectedValue & " and   section_name = '" & Me.TxtName.Text & "'"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_section", "section")
                If DS.Tables(0).Rows.Count <> 0 Then
                    MySectionID = DS.Tables(0).Rows(0).Item("section_id").ToString()
                End If
                Strsql = "Insert into category (Category_name,note,siteid,sectionid) values ('ไม่ระบุ','ค่าเริ่มต้น'," & Me.CboSite.SelectedValue & "," & MySectionID & ")"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)

                MsgBox("เพิ่มข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
            Else

                Strsql = "update section set section_name= '" & Me.TxtName.Text & "', note = '" & Me.TxtNote.Text & "' ,active = " & Me.ChkActive.Checked
                Strsql = Strsql & " where section_id = " & Me.Txtid.Text.Trim
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)


                Strsql = "select * from  category where siteid = " & Me.CboSite.SelectedValue & " and   sectionid = " & Me.Txtid.Text.Trim
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_category", "category")
                If DS.Tables(0).Rows.Count = 0 Then
                    Strsql = "Insert into category (Category_name,note,siteid,sectionid) values ('ไม่ระบุ','ค่าเริ่มต้น'," & Me.CboSite.SelectedValue & "," & Me.Txtid.Text.Trim & ")"
                    Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                    Mysql1.MySQLExecute(Strsql)
                End If
            End If

            'Refresh Data
            MsgBox("ปรับปรุงข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)



        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click
        Me.Txtid.Text = ""
        Me.TxtName.Text = ""
        Me.TxtNote.Text = ""
        Me.ChkActive.Checked = False
    End Sub


    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub


   
End Class